package com.team7.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team7.common.entity.Link;
import com.team7.common.vo.CommonResult;
import com.team7.common.vo.LinkVo;
import com.team7.home.dao.LinkDao;
import com.team7.home.service.LinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张煜杰
 * @since 2021-10-21
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkDao, Link> implements LinkService {
    @Resource
    private LinkDao linkDao;

    @Override
    public CommonResult getAllLink(Integer page, Integer limit, LinkVo linkVo) {
        //获取分页插件
        Page<Link> bannerPage = new Page<>(page,limit);
        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.isNotEmpty(linkVo.getLinkName())){
            wrapper.like("link_name",linkVo.getLinkName());
        }
        if (linkVo.getIsDisable()!=null){
            wrapper.eq("is_disable",linkVo.getIsDisable());
        }
        Page<Link> selectPage = linkDao.selectPage(bannerPage, wrapper);
        if (selectPage.getRecords().size()>0){
            return new CommonResult(2000,"查询成功",selectPage);
        }else{
            return new CommonResult(5000,"查询失败，当前没有数据",null);
        }
    }

    @Override
    public CommonResult changeStatus(String linkId, Integer isDisable) {
        Link link = new Link();
        link.setLinkId(linkId);
        link.setIsDisable(isDisable);
        int i = linkDao.updateById(link);
        if (i>0){
            return new CommonResult(2000,"改变成功",null);
        }else {
            return new CommonResult(5000,"改变失败",null);
        }
    }

    @Override
    public CommonResult deleteLink(String linkId) {
        Link link = new Link();
        link.setLinkId(linkId);
        link.setIsDeleted(1);
        int i = linkDao.updateById(link);
        if (i>0){
            return new CommonResult(2000,"删除成功",null);
        }else {
            return new CommonResult(5000,"删除失败",null);
        }
    }

    @Override
    public CommonResult addLink(LinkVo linkVo) {
        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        Link link = new Link();
        //根据传来的名称判断数据库中是否有数据
        String linkName = linkVo.getLinkName();
        wrapper.eq("link_name", linkName);
        Link selectOne = linkDao.selectOne(wrapper);
        //判断id是否为空，如果为空，代表增加方法，如果不为空代表更新方法
        if (StringUtils.isNotEmpty(linkVo.getLinkId())){
            link.setLinkId(linkVo.getLinkId());
            link.setGmtModified( LocalDateTime.now());
            link.setLinkUrl(linkVo.getLinkUrl());
            link.setLinkName(linkVo.getLinkName());
            int i = linkDao.updateById(link);
            if (i>0){
                return new CommonResult(2000,"更新成功",null);
            }else {
                return new CommonResult(5000,"未知原因更新失败",null);
            }
        }else{
            if (selectOne!=null){
                return new CommonResult(5000,"添加失败，该链接名称已存在",null);
            }else{
                link.setGmtCreate( LocalDateTime.now());
                link.setGmtModified( LocalDateTime.now());
                link.setLinkUrl(linkVo.getLinkUrl());
                link.setLinkName(linkVo.getLinkName());
                link.setIsDisable(0);
                link.setIsDeleted(0);
                int i = linkDao.insert(link);
                if (i>0){
                    return new CommonResult(2000,"添加成功",null);
                }else {
                    return new CommonResult(5000,"未知原因添加失败",null);
                }
            }
        }
    }

    /**
     * 获取友情链接并返回到前端
     * @return
     */
    @Override
    public CommonResult getQianLink() {
        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        wrapper.eq("is_disable",0);
        wrapper.eq("is_deleted",0);
        List<Link> links = linkDao.selectList(wrapper);
        return new CommonResult(2000,"查询成功",links);
    }

}
